﻿//Copyrigh (C) 2021 上海东欣软件工程有限公司 版权所有。
//<author>邢耀光</author>
//<data>2021.1.31</data>

var updateUserDepID;//标记修改时的部门ID，自动读取和用户选择属性值来源不同
var departmentID = 0;//全局变量，当前部门ID
var departmentName = "东欣公司";
//用户数据模型
Ext.define('userModel', {
    extend: 'Ext.data.Model',
    fields: [{ name: 'ID', type: "int" },
    { name: 'Name' },
    { name: "Description" },
    { name: "DepartmentID" },
    { name: "DepartmentName" },
    { name: "IsSystemOnly" }]
});

//用户数据商店
var userStore = Ext.create('Ext.data.Store', {
    storeId: 'userStore',
    model: "userModel",
    pageSize:20,
    proxy: {
        type: 'ajax',
        url: 'Home/GetAllUserData',
        reader: {
            type: 'json',
            root: 'Value',
            totalProperty:'TotalRows'
        }
    }
    //,
    //autoLoad: true
});
userStore.on('beforeload', function (store, options) {
    var userName = Ext.getCmp('txtSearchName').getValue();
    var userDesc = Ext.getCmp('txtSearchDescription').getValue();
    var userDepID = parseInt(departmentID);
    Ext.apply(store.proxy.extraParams, { userName: userName, userDesc: userDesc, userDepID: userDepID })
});
function LoadDataStore() {
    userStore.loadPage(1)
}
//EXT入口
Ext.onReady(function () {
    //显示快速提示信息
    Ext.QuickTips.init();  
    //部门商店
    var departmentStore = Ext.create('Ext.data.TreeStore', {
        id: 'departmentStore',
        proxy: {
            type: 'ajax',
            url: 'Home/GetAllDepartmentData',
            reader: {
                type: 'json'
            }
        },
        listeners: {
            load: function (node) {
                //监听初次展开根节点
                var childNode = departmentStore.getRootNode().childNodes[0];
                Ext.getCmp('demoDepartmentTree').getSelectionModel().select(childNode);
                departmentID = childNode.data.id
                if (childNode != undefined) {
                    childNode.expand();
                }
            }
        }
    });
    //用户列表
    var demoGrid = Ext.create('Ext.grid.Panel', {
        store: userStore, autoScroll: true, id: "demoGrid",
        title: "用户列表",
        tools: [{
            xtype: "button", text: "添加", id: "btnCreate", margin: "0 30 0 0",
            listeners: {
                click: function () {
                    Ext.Msg.confirm("提示:", "确定在当前部门:" + departmentName + "下，创建用户数据?", function (e) {
                        if (e == "yes") {
                            var createUser = Ext.create('Ext.window.Window', {
                                title: '添加用户',
                                height: 180,
                                width: 350,
                                modal: true,
                                layout: 'vbox',
                                items: [
                                    { xtype: "textfield", fieldLabel: '用户名称', labelStyle: 'color:red', labelAlign: "right", id: "txtUserName", allowBlank: false, margin: "20 0 0 0" },
                                    { xtype: 'textfield', fieldLabel: '描述', labelAlign: "right", id: "txtUserDescription", margin: "10 0 0 0 " }],
                                buttonAlign: "center",
                                buttons: [{
                                    text: "确定", handler: function () {
                                        if (!Ext.getCmp('txtUserName').isValid()) {
                                            MessageBox(Ext.getCmp('txtUserName').getActiveError(), "ERROR", function () {
                                                Ext.getCmp('txtUserName').focus();
                                            });
                                            return;
                                        }
                                        Ext.Ajax.request({
                                            url: 'Home/CreateUserData',
                                            params: {
                                                userName: Ext.getCmp('txtUserName').getValue(),
                                                userDesc: Ext.getCmp('txtUserDescription').getValue(),
                                                userDepID: parseInt(departmentID)
                                            },
                                            success: function (response) {
                                                var result = Ext.JSON.decode(response.responseText);
                                                if (result.success) {
                                                    MessageBox(result.msg, "INFO", function () {
                                                        createUser.close();
                                                        //清空搜索条件
                                                        Ext.getCmp('txtSearchName').reset();
                                                        Ext.getCmp('txtSearchDescription').reset();
                                                        //刷新数据
                                                        SearchData();
                                                    });
                                                }
                                                else {
                                                    MessageBox(result.msg, "ERROR");
                                                }
                                            }
                                        });
                                    }
                                },
                                {
                                    text: "取消", handler: function () {
                                        createUser.close();
                                    }
                                }]
                            }).show();
                        }
                    })                    
                }
            }
        },
            {
                xtype: "button", text: "修改", id: "btnUpdate", margin: "0 30 0 0",
                listeners: {
                    click: function () {
                        var getSelectData = Ext.getCmp("demoGrid").getSelectionModel().selected;
                        if (getSelectData.length <= 0) {
                            MessageBox("请先选择用户", "ERROR");
                            return;
                        }
                        if (getSelectData.items[0].data.IsSystemOnly == true) {
                            MessageBox("系统用户不可修改", "ERROR");
                            return;
                        }

                        var updateUser = Ext.create('Ext.window.Window', {
                            title: '修改用户',
                            modal:true,
                            height: 220,
                            width: 350,
                            layout: 'vbox',
                            items: [
                            {
                                xtype: 'container', layout: 'hbox', items: [
                                    { xtype: "textfield", fieldLabel: '部门', labelStyle: 'color:red', width: 253, labelAlign: "right", id: "txtUpdateUserDepID", allowBlank: false, readOnly: true, margin: "10 0 0 0" },
                                    {
                                        xtype: "button", text: '...', id: "btntUpdateUserDepID", margin: "10 0 0 0",
                                        listeners: {
                                            click: function () {
                                                Ext.getCmp('txtUpdateUserDepID').reset();
                                                Ext.getCmp('txtUpdateUserDepID').data = '';
                                                //显示部门列表
                                                ShowSelectDepartment('txtUpdateUserDepID');                                               
                                            }
                                        }
                                    }
                                ]
                            },
                                { xtype: "textfield", fieldLabel: '用户', labelStyle: 'color:red',labelAlign: "right", id: "txtUpdateUserName", allowBlank: false, margin: "20 0 0 0" },
                                { xtype: 'textfield', fieldLabel: '描述', labelAlign: "right", id: "txtUpdateUserDescription", margin: "10 0 0 0 " }],
                            buttonAlign: "center",
                            buttons: [{
                                text: "确定", handler: function () {
                                    if (!Ext.getCmp('txtUpdateUserDepID').isValid()) {
                                        MessageBox(Ext.getCmp('txtUpdateUserDepID').getActiveError(), "ERROR", function () {
                                            Ext.getCmp('txtUpdateUserDepID').focus();
                                        });
                                        return;
                                    }
                                    if (!Ext.getCmp('txtUpdateUserName').isValid()) {
                                        MessageBox(Ext.getCmp('txtUpdateUserName').getActiveError(), "ERROR", function () {
                                            Ext.getCmp('txtUpdateUserName').focus();
                                        });
                                        return;
                                    }
                                    Ext.Ajax.request({
                                        url: 'Home/UpdateUserData',
                                        params: {
                                            userId: getSelectData.items[0].data.ID,
                                            userName: Ext.getCmp('txtUpdateUserName').getValue(),
                                            userDesc: Ext.getCmp('txtUpdateUserDescription').getValue(),
                                            userDepID: parseInt(updateUserDepID)
                                        },
                                        success: function (response) {
                                            var result = Ext.JSON.decode(response.responseText);
                                            if (result.success) {
                                                MessageBox(result.msg, "INFO", function () {
                                                    SearchData();
                                                    updateUser.close();                                                   
                                                });
                                            }
                                            else {
                                                MessageBox(result.msg, "ERROR");
                                            }
                                        }
                                    });
                                }
                            },
                            {
                                text: "取消", handler: function () {
                                    updateUser.close();
                                }
                            }]
                        }).show();
                        Ext.getCmp('txtUpdateUserName').setValue(getSelectData.items[0].data.Name);
                        Ext.getCmp('txtUpdateUserDescription').setValue(getSelectData.items[0].data.Description);
                        Ext.getCmp('txtUpdateUserDepID').setValue(getSelectData.items[0].data.DepartmentName);
                        Ext.getCmp('txtUpdateUserDepID').data = getSelectData.items[0].data;
                        //自动读取时
                        updateUserDepID = getSelectData.items[0].data.DepartmentID;

                    }
                }
            },
            {
                xtype: "button", text: "删除", id: "btnDelete", margin: "0 30 0 0",
                listeners: {
                    click: function () {
                        Ext.Msg.confirm("提示:", "确定删除选定的用户?", function (e) {
                            if (e == "yes") {
                                var getSelectData = Ext.getCmp("demoGrid").getSelectionModel().selected;
                                if (getSelectData.length <= 0) {
                                    MessageBox("请先选择用户", "ERROR");
                                    return;
                                }
                                if (getSelectData.items[0].data.IsSystemOnly == true) {
                                    MessageBox("系统用户不可删除", "ERROR");
                                    return;
                                }
                                Ext.Ajax.request({
                                    url: 'Home/DeleteUserData',
                                    params: {
                                        userId: getSelectData.items[0].data.ID
                                    },
                                    success: function (response) {
                                        var result = Ext.JSON.decode(response.responseText);
                                        if (result.success) {
                                            MessageBox(result.msg, "INFO", function () {
                                                SearchData();
                                            });
                                        }
                                        else {
                                            MessageBox(result.msg, "ERROR");
                                        }
                                    }
                                });
                            }
                        });
                        
                    }
                }
            },
            {
                xtype: "button", text: "导入", id: "btnImport", margin: "0 30 0 0",
                listeners: {
                    click: function () {
                        var importPanel = Ext.create('Ext.form.Panel', {
                            id: "importPanel",
                            bodyStyle: 'background-color:white',
                            layout: "vbox",
                            items: [
                                {
                                    xtype: "container", layout: "hbox", margin: "5 0 0 0",
                                    items: [
                                        {
                                            xtype: "textfield", fieldLabel: "选择部门", labelStyle: 'color:red', width: 252, id: "importDepartmentID", margin: '10 0 0 10',
                                            labelWidth: 60, labelAlign: 'right', allowBlank: false, readOnly: true
                                        },
                                        {
                                            xtype: "button", text: '...', id: "btnDepID", margin: "10 0 0 0",
                                            listeners: {
                                                click: function () {
                                                    Ext.getCmp('importDepartmentID').reset();
                                                    Ext.getCmp('importDepartmentID').data = '';
                                                    //显示部门列表
                                                    ShowSelectDepartment('importDepartmentID');
                                                }
                                            }
                                        }
                                    ]
                                },
                                {
                                    xtype: 'container', layout: 'hbox', margin: '5 0 0 0',
                                    items: [
                                        {
                                            xtype: 'filefield', fieldLabel: "选择文件", labelStyle: 'color:red', msgTarget: 'side', width: 400, id: "importUserData", margin: '10 0 0 10',
                                            labelWidth: 60, labelAlign: 'right', allowBlank: false, buttonText: '浏览...',
                                            validator: function (value) {
                                                if (value != "") {
                                                    var arr = value.split(".");
                                                    if (arr[arr.length - 1].toLowerCase() != "xls" && arr[arr.length - 1].toLowerCase() != "xlsx") {
                                                        return "选定文件不合法"
                                                    } else {
                                                        return true;
                                                    }
                                                }
                                            }
                                        },
                                        {
                                            xtype: "button", text: '导入', margin: "10 0 0 20",
                                            listeners: {
                                                click: function () {
                                                    //if (!Ext.getCmp('importDepartmentID').isValid()) {
                                                    //    MessageBox(Ext.getCmp('importDepartmentID').getActiveError(), "ERROR", function () {
                                                    //        Ext.getCmp('importDepartmentID').focus();
                                                    //    });
                                                    //    return;
                                                    //}
                                                    if (!Ext.getCmp('importDepartmentID').isValid()) {
                                                        MessageBox("请先选择部门", "ERROR", function () {
                                                            Ext.getCmp('importDepartmentID').focus();
                                                        });
                                                        return;
                                                    }
                                                    importDepartmentID = Ext.getCmp('importDepartmentID').data.id;
                                                    var form = this.up('form').getForm();
                                                    if (form.isValid()) {
                                                        form.submit({
                                                            url: 'Home/ImportExcel',
                                                            params: {
                                                                departmentID: parseInt(importDepartmentID),
                                                                filePath: "C:/Users/xyg/Documents/工作簿2.xlsx"
                                                            },
                                                            success: function (fp, res) {
                                                                waitMsg: '数据导入中......';
                                                                var result = Ext.JSON.decode(res.response.responseText);
                                                                if (result.success) {
                                                                    MessageBox(result.msg, "INFO");
                                                                }
                                                                else {
                                                                    MessageBox(result.msg, "ERROR");
                                                                }
                                                            }
                                                        })
                                                    }
                                                }
                                            }
                                        }
                                    ]
                                }
                            ]
                        });
                        Ext.create("Ext.window.Window", {
                            title: '导入部门用户数据',
                            height: 130,
                            width: 500,
                            layout: 'fit',
                            modal: true,
                            resizable: false,
                            items:[importPanel]
                        }).show();
                    }
                }
            },
            {
                xtype: "button", text: "导出", id: "btnExport", margin: "0 10 0 0",
                listeners: {                    
                    click: function () {
                        Ext.Msg.confirm("提示:", "确定导出部门:" + departmentName +"?", function (e) {
                            if (e == "yes") {
                                Ext.Ajax.request({
                                    url: 'Home/ExportData',
                                    params: {
                                        departmentID: parseInt(departmentID)
                                    },
                                    success: function (response) {
                                        var result = Ext.JSON.decode(response.responseText);
                                        if (result.success) {
                                            MessageBox(result.msg, "INFO");
                                        }
                                        else {
                                            MessageBox(result.msg, "ERROR");
                                        }
                                    }
                                });
                            }
                        })
                    }
                }
            }],
        columns: [{ xtype: 'rownumberer', width: 50 },
            { text: 'ID', dataIndex: 'ID', hidden: true, hideable: false },            
            { text: '用户', dataIndex: 'Name', flex: 1 },
            { text: '所属部门', dataIndex: 'DepartmentName', flex: 1 },
            { text: '描述', dataIndex: 'Description', flex: 1 },
            { text: '是否系统', dataIndex: 'IsSystemOnly',
                renderer: function (value) {
                    return value == '0' ? '否' : '是';
                }
            }
        ],
        dockedItems: [{
            xtype: 'pagingtoolbar',
            store: userStore,
            dock: 'bottom',
            id: "Paging",
            displayInfo:true
        }]
    });
    //部门树列表
    var demoDepartmentTree = Ext.create('Ext.tree.Panel', {
        store: departmentStore,
        autoScroll: true,
        split: true,
        frame: true,// 美化界面
        containerScroll: true,// 是否支持滚动条
        border: false, // 边框 
        animate: true, // 动画效果 
        id: "demoDepartmentTree",
        title: "部门-->用户管理",
        rootVisible: false,
        singleExpand: true,
        multSelect: false,
        width: 175,
        dockedItems: [{
            dock: 'top',
            xtype: 'toolbar',
            items: {
                xtype: 'triggerfield',
                id: 'searchText',
                emptyText: '部门检索',
                width: 155,
                triggerCls: 'x-form-search-trigger',
                onTriggerClick: function () {
                    var searchText = Ext.getCmp('searchText').getValue();
                    var departments = Ext.getCmp('demoDepartmentTree');
                    SearchTree(departments, searchText);
                }
            }
        }],
        listeners: {
            'itemexpand': EventEexpand,
            'itemclick': function (view, record, item, index, e) {
                if (record != undefined) {
                    //重新定位用户列表
                    departmentID = record.data.id
                    departmentName = record.data.text;
                    Redirect(departmentID);
                }
            }
        }
    });
    // 部门树增加右键点击事件 
    demoDepartmentTree.on('itemcontextmenu', function (view, recode, item, index, event) {
        // 添加一个节点（叶子）
        var chlidNodeClickMenu = new Ext.menu.Menu({
                items: [
                    {
                        text: '新增部门',
                        iconCls: 'leaf',
                        // 增加点击事件 
                        handler: function () {
                            Ext.MessageBox.prompt("请输入增加部门名称", "", function (e, text) {
                                if (e == "ok") {
                                    Ext.Ajax.request({
                                        url: 'Home/CreateDepartmentData',
                                        params: {
                                            parentID: parseInt(recode.data.id),
                                            name: text
                                        },
                                        success: function (response) {
                                            var result = Ext.JSON.decode(response.responseText);
                                            if (result.success) {
                                                MessageBox(result.msg, "INFO", function () {
                                                    //新增成功后，前台加载树节点的子节点
                                                    recode.set('loading', true);
                                                    Ext.Ajax.request({
                                                        loadMask: true,
                                                        url: 'Home/GetAllDepartmentDataByID',
                                                        params: {
                                                            id: parseInt(recode.data.id)
                                                        },
                                                        success: function (response) {
                                                            var result = Ext.JSON.decode(response.responseText);
                                                            //前台新增节点父节点清空数据，并添加新子节点数据，并展开
                                                            if (result.length > 0) {
                                                                recode.removeAll();
                                                                recode.appendChild(result)
                                                                recode.set('loading', false);
                                                                // 增加新结点后，父节点置为非叶子节点
                                                                recode.data.leaf = false;
                                                                // 父节点展开
                                                                recode.expand();
                                                                //重新选定新添加的叶子节点
                                                                demoDepartmentTree.getSelectionModel().select(recode.childNodes[recode.childNodes.length - 1])
                                                                // 全局变量搜索部门id重新赋值
                                                                departmentID = recode.childNodes[recode.childNodes.length - 1].data.id;
                                                                //并根据新增加部门刷新用户数据
                                                                userStore.removeAll();
                                                            }
                                                            else {
                                                                recode.set('loading', false);
                                                            }
                                                        }
                                                    });
                                                });
                                            }
                                            else {
                                                MessageBox(result.msg, "ERROR");
                                            }
                                        }
                                    });
                                    
                                }
                            });
                        }
                    },
                    {
                        text: '修改部门',
                        iconCls: 'leaf',
                        handler: function () {
                            Ext.MessageBox.prompt("请输入要修改部门新名称", "", function (e, text) {
                                if (e == "ok") {
                                    //根节点操作
                                    if (recode.data.id == 1) {0
                                        Ext.Ajax.request({
                                            url: 'Home/UpdateDepartmentData',
                                            params: {
                                                id: parseInt(recode.data.id),
                                                parentID: parseInt(0),
                                                name: text
                                            },
                                            success: function (response) {
                                                var result = Ext.JSON.decode(response.responseText);
                                                if (result.success) {
                                                    MessageBox(result.msg, "INFO", function () {
                                                        //刷新数据        
                                                        departmentStore.reload();
                                                        userStore.reload();
                                                    });
                                                }
                                                else {
                                                    MessageBox(result.msg, "ERROR");
                                                }
                                            }
                                        });
                                    }
                                    //非根节点操作
                                    else {
                                        Ext.Ajax.request({
                                            url: 'Home/UpdateDepartmentData',
                                            params: {
                                                id: parseInt(recode.data.id),
                                                parentID: parseInt(recode.data.parentId),
                                                name: text
                                            },
                                            success: function (response) {
                                                var result = Ext.JSON.decode(response.responseText);
                                                if (result.success) {
                                                    MessageBox(result.msg, "INFO", function () {
                                                        //前台修改节点后，重新加载父节点，清空并刷新数据，然后展开
                                                        //修改节点的父节点
                                                        var node = recode.parentNode;
                                                        //加载树节点的子节点
                                                        node.set('loading', true);
                                                        Ext.Ajax.request({
                                                            loadMask: true,
                                                            url: 'Home/GetAllDepartmentDataByID',
                                                            params: {
                                                                id: parseInt(recode.parentNode.data.id)
                                                            },
                                                            success: function (response) {
                                                                var result = Ext.JSON.decode(response.responseText);
                                                                if (result.length > 0) {
                                                                    node.removeAll();
                                                                    node.appendChild(result)
                                                                    node.set('loading', false);
                                                                    node.expand();
                                                                    //重新选定新修改的叶子节点
                                                                    demoDepartmentTree.getSelectionModel().select(recode)
                                                                    //并根据新修改部门刷新用户数据
                                                                    SearchData();
                                                                }
                                                                else {
                                                                    node.set('loading', false);
                                                                }
                                                            }
                                                        });
                                                    });
                                                }
                                                else {
                                                    MessageBox(result.msg, "ERROR");
                                                }
                                            }
                                        });
                                    }
                                   
                                }
                            });
                        }
                    },
                    {
                        text: '删除部门',
                        iconCls: 'leaf',
                        handler: function () {
                            Ext.Msg.confirm("提示:", "确定删除选定的部门?", function (e) {
                                if (e == "yes") {
                                    //根节点操作
                                    if (recode.data.id == 1) {
                                        MessageBox("根节点部门，不能删除", "ERROR");
                                    }
                                    //非根节点操作
                                    else {
                                        if (recode.data.leaf == true) {
                                            Ext.Ajax.request({
                                                loadMask: true,
                                                url: 'Home/SearchUserData',
                                                params: {
                                                    userName: '',
                                                    userDesc: '',
                                                    userDepID: parseInt(recode.data.id)
                                                },
                                                success: function (response) {
                                                    var result = Ext.JSON.decode(response.responseText);
                                                    if (result.success) {
                                                        var getData = Ext.JSON.decode(result.msg);
                                                        if (getData.length > 0) {
                                                            MessageBox("部门存在关联用户数据，不能删除", "ERROR");
                                                        }
                                                        else {
                                                            Ext.Ajax.request({
                                                                url: 'Home/DeleteDepartmentData',
                                                                params: {
                                                                    id: parseInt(recode.data.id)
                                                                },
                                                                success: function (response) {
                                                                    var result = Ext.JSON.decode(response.responseText);
                                                                    if (result.success) {
                                                                        MessageBox(result.msg, "INFO", function () {
                                                                            //删除节点的父节点
                                                                            var node = recode.parentNode;
                                                                            //如果父节点只有一条数据时，最后一条数据删除后，把父节点置为叶子节点
                                                                            if (node.childNodes.length == 1) {
                                                                                node.data.leaf = true;
                                                                            }
                                                                            //删除节点
                                                                            recode.remove();
                                                                            //父节点展开
                                                                            node.expand();
                                                                            //选择父节点
                                                                            demoDepartmentTree.getSelectionModel().select(node)
                                                                            departmentID = node.data.id;
                                                                            //刷新部门用户数据
                                                                            SearchData();
                                                                        });
                                                                    }
                                                                    else {
                                                                        MessageBox(result.msg, "ERROR");
                                                                    }
                                                                }
                                                            });
                                                        }
                                                    }
                                                    else {
                                                        MessageBox("用户查询失败", "ERROR");
                                                    }
                                                }
                                            });
                                        }
                                        else {
                                            MessageBox("存在子部门，不能删除", "ERROR");
                                        }
                                    }                            
                                }
                            });
                        }
                    }]
            });
        chlidNodeClickMenu.showAt(event.getPoint());//menu的showAt，不要忘记        
        event.preventDefault();// 阻止浏览器默认右键菜单显示
    });
    //创建渲染总界面
    Ext.create('Ext.container.Viewport', {
        layout: 'border',
        items: [
            {
                region: 'west', layout: "fit",
                margin: "45 0 0 0",
                width: 240,
                items: [demoDepartmentTree]
            },
            {
                region: 'center', layout: 'border',    
                margin: "45 0 0 0",
                items: [{
                    region: 'north', height: 80, layout: "hbox",
                    items: [
                    { xtype: "textfield", fieldLabel: '用户', labelAlign: "right", id: "txtSearchName", allowBlank: true, margin: "30 0 0 0" },
                    { xtype: 'textfield', fieldLabel: '描述', labelAlign: "right", id: "txtSearchDescription", margin: "30 0 0 0" },
                    {
                        xtype: "button", text: "查询", id: "btnSearch", margin: "30 0 0 30",
                        listeners: {
                            click: function () {
                                SearchData();
                            }
                        }
                    }]
                },
                {
                    region: 'center', layout: 'fit',
                    items: [demoGrid]
                }]
        }]
    });
    userStore.loadPage(1);
});
// 搜索部门用户数据
function SearchData() {
    userStore.loadPage(1)
    //var myMask = new Ext.LoadMask(Ext.getBody(), { msg: "请等候..." });
    //myMask.show();
    //Ext.Ajax.request({
    //    loadMask: true,
    //    url: 'Home/SearchUserData',
    //    params: {
    //        userName: Ext.getCmp('txtSearchName').getValue(),
    //        userDesc: Ext.getCmp('txtSearchDescription').getValue(),
    //        userDepID: parseInt(departmentID)
    //    },
    //    success: function(response) {
    //        var result = Ext.JSON.decode(response.responseText);
    //        if (result.success) {
    //            var getData = Ext.JSON.decode(result.msg);
    //            userStore.loadData(getData.Value);
    //            myMask.hide();
    //        }
    //        else {
    //            myMask.hide();
    //            MessageBox("用户查询失败", "ERROR");
    //        }
    //    }
    //});
}

//遍历部门树，用于部门树的检索
function SearchTree(objectTree, searchtext) {
    var rootNode = objectTree.getRootNode();//获取根节点
    Findchildnode(objectTree, rootNode, searchtext);//开始递归
}
//获取所有子节点
function Findchildnode(objectTree, rootNode, searchtext) {
    if (searchtext == '根') searchtext = '所有';
    var childnodes = rootNode.childNodes;
    var ms = objectTree.selModel;
    Ext.each(childnodes, function () {//从节点中取出子节点依次遍历
        var nd = this;
        if (nd.data.text.search(searchtext) != -1) {
            ms.selected.removeAll(true);//清空原来的选择项
            ms.select(nd);//设置新的选择项
            //重新定位用户列表
            departmentID = nd.data.id;
            departmentName = nd.data.text;
            Redirect(departmentID);
            nd.parentNode.expand();
            return false;
        }
        else {
            if (nd.hasChildNodes()) {//判断子节点下是否存在子节点
                Findchildnode(objectTree, nd, searchtext)//如果存在子节点，递归
            }
        }
    })    
}

//根据部门id重定向刷新对应部门用户列表数据
function Redirect(id) {
    Ext.getCmp('txtSearchName').reset();
    Ext.getCmp('txtSearchDescription').reset();
    userStore.loadPage(1)
    //Ext.Ajax.request({
    //    url: 'Home/SearchUserData',
    //    params: {
    //        userName: '',
    //        userDesc: '',
    //        userDepID: id
    //    },
    //    success: function (response) {
    //        var result = Ext.JSON.decode(response.responseText);
    //        if (result.success) {
    //            var getData = Ext.JSON.decode(result.msg);
    //            userStore.loadData(getData);
    //        }
    //        else {
    //            MessageBox("用户查询失败", "ERROR");
    //        }
    //    }
    //});
}

//显示选择部门列表
function ShowSelectDepartment(controlId) {
    Ext.define('depModel', {
        extend: 'Ext.data.Model',
        fields: [{ name: 'id', type: "int" },
        { name: 'text' },
        { name: "leaf" },
        { name: "children" }]
    });
    //部门Store
    var depStore = Ext.create('Ext.data.TreeStore', {
        id: 'depStore',
        proxy: {
            type: 'ajax',
            url: 'Home/GetAllDepartmentData',
            reader: {
                type: 'json'
            }
        },
        listeners: {
            load: function (node) {
                var childNode = depStore.getRootNode().childNodes[0];
                Ext.getCmp('depTree').getSelectionModel().select(childNode);
                if (childNode != undefined) {
                    childNode.expand();
                }
            }
        }
    });
    // 部门树
    var depTree = Ext.create('Ext.tree.Panel', {
        store:depStore,
        autoScroll: true,
        id: 'depTree',
        rootVisible: false,
        singleExpand: true,
        multSelect: false,
        width: 175,
        listeners: {
            'itemexpand': EventEexpand            
        }
    });
    //部门信息界面
    var departmentWin = Ext.create('Ext.window.Window', {
        title: '部门信息',
        height: 500,
        width: 300,
        modal: true,
        resizable: false,
        closeAction: 'hide',
        layout: 'border',
        items: [{
            region: 'center',
            border: 'false',
            width: 260,
            layout: 'fit',
            items: [depTree]
        }],
        buttonAlign: 'center',
        buttons: [{
            text: '确定', listeners: {
                click: function () {
                    var selectData = Ext.getCmp('depTree').getSelectionModel().selected;
                    if (selectData.length <= 0) {
                        MessageBox('请先选择部门', "ERROR");
                    }
                    else {
                        Ext.getCmp(controlId).setValue(selectData.items[0].data.text);
                        Ext.getCmp(controlId).data = selectData.items[0].data;
                        //用户选择时
                        updateUserDepID = selectData.items[0].data.id;
                        departmentWin.close();
                    }
                }
            }
        }, {
            text: '取消', margin: "0 0 0 20", listeners: {
                click: function () {
                    departmentWin.close();
                }
            }
        }]
    }).show();
}

//展开树节点
function EventEexpand(recode, eopts) {
    if (recode.childNodes.length > 0) {
        for (var i = 0; i < recode.childNodes.length; i++) {
            if (recode.childNodes[i].get('text') == '假节点') {
                recode.removeAll();
                break;
            }
        }
    }
    var id = recode.get('id');
    if (!recode.get('leaf') && recode.childNodes.length == 0) {
        //加载树节点的子节点
        recode.set('loading', true);
        Ext.Ajax.request({
            loadMask: true,
            url: 'Home/GetAllDepartmentDataByID',
            params: {
                id: parseInt(id)
            },
            success: function (response) {
                var result = Ext.JSON.decode(response.responseText);
                if (result.length>0) {
                    recode.appendChild(result)
                    recode.set('loading', false);
                    recode.expand();                }
                else {
                    recode.set('loading', false);
                }
            }
        });

    }
}